package cn.gogoyc.org.controller;

import cn.gogoyc.org.basic.PreAuthorize;
import cn.gogoyc.org.basic.query.BaseQuery;
import cn.gogoyc.org.service.IShopAuditLogService;
import cn.gogoyc.org.domain.ShopAuditLog;
import cn.gogoyc.org.query.ShopAuditLogQuery;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 店铺日志接口类；
 */
@RestController
@RequestMapping("/shopAuditLog")
public class ShopAuditLogController {

    /**
     * The Shop audit log service.
     */
    @Autowired
    public IShopAuditLogService shopAuditLogService;

    /**
     * 接口：日志添加或修改
     *
     * @param shopAuditLog 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "店铺日志添加或删除",sn = "ShopAuditLog:updateAndSave")
    public AjaxResult addOrUpdate(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if( shopAuditLog.getId()!=null) {
                shopAuditLogService.update(shopAuditLog);
            }
            else {
                shopAuditLogService.save(shopAuditLog);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("操作失败",false);
        }
    }

    /**
     * 接口：日志删除
     *
     * @param id the id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "店铺日志删除",sn = "ShopAuditLog:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopAuditLogService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult("操作失败",false);
        }
    }

    /**
     * 接口：日志批量删除
     *
     * @param ids the ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    @PreAuthorize(name = "店铺日志批量删除",sn = "ShopAuditLog:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopAuditLogService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("批量删除失败",false);
        }
    }

    /**
     * 接口：日志查询单个对象
     *
     * @param id the id
     * @return the shop audit log
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个店铺日志",sn = "ShopAuditLog:getOne")
    public ShopAuditLog findOne(@PathVariable("id")Long id) {
        return shopAuditLogService.loadById(id);
    }


    /**
     * 接口：日志查询所有
     *
     * @return list
     */
    @GetMapping
    @PreAuthorize(name = "查询所有店铺日志",sn = "ShopAuditLog:getAll")
    public List<ShopAuditLog> findAll(){
        return shopAuditLogService.loadAll();
    }


    /**
     * 接口：日志分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    @PreAuthorize(name = "店铺日志分页列表",sn = "ShopAuditLog:list")
    public PageList<ShopAuditLog> queryPage(@RequestBody BaseQuery query) {
        return shopAuditLogService.queryPage(query);
    }
}
